package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.ljfiction.book.Dialog.IsOrNoDialog;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserBean;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.GlideCacheUtil;
import com.ljfiction.book.tools.PreferencesTool;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.Tool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class SetupActivity extends BaseActivity {

    @BindView(R.id.backimageset)
    ImageView backimageset;

    @BindView(R.id.aboutUsLL)
    LinearLayout aboutUsLL;
    @BindView(R.id.feedbackLL)
    LinearLayout feedbackLL;

    @BindView(R.id.versionTV)
    TextView versionTV;
    @BindView(R.id.exitBut)
    SuperButton exitBut;
    @BindView(R.id.cleanLL)
    LinearLayout cleanLL;
    private IsOrNoDialog isOrNoDialog;
    private Intent intent;
    private Intent intent1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        ButterKnife.bind(this);
        intent = new Intent(this, FeedbackActivity.class);
        intview();
    }

    private void intview() {
        if (UserManageBean.isLogin()) {
            exitBut.setVisibility(View.VISIBLE);
        } else {
            exitBut.setVisibility(View.GONE);
        }
        versionTV.setText(Tool.getVersion(this));
    }

    @OnClick({R.id.backimageset, R.id.cleanLL,  R.id.aboutUsLL, R.id.feedbackLL, R.id.exitBut})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backimageset:
                goBack();
                break;
            case R.id.cleanLL:
                long qlsize=0;
                if (getExternalCacheDir()!=null){
                  qlsize=  getFolderSize(getExternalCacheDir());
                }
                isOrNoDialog = new IsOrNoDialog(this, "确定清除" +getFormatSize(getFolderSize(new File(getCacheDir() + "/"+ InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR))+qlsize)
                        + "缓存", true);
                isOrNoDialog.setListener(new IsOrNoDialog.Listener() {
                    @Override
                    public void okClick() {
                        if (getExternalCacheDir() != null && getExternalCacheDir().exists() && getExternalCacheDir().isDirectory()) {
                            for (File item : getExternalCacheDir().listFiles()) {
                                item.delete();
                            }
                        }
                        GlideCacheUtil.clearImageAllCache(SetupActivity.this);
                        showToast("缓存清除成功");
                    }

                    @Override
                    public void closeClick() {

                    }
                });
                break;
            /*case R.id.callLL:
                isOrNoDialog = new IsOrNoDialog(this, "是否拨打0571-85120281客服电话", true);
                isOrNoDialog.setListener(new IsOrNoDialog.Listener() {
                    @Override
                    public void okClick() {
                        Tool.call(getBaseContext(), "0571-85120281");
                    }

                    @Override
                    public void closeClick() {

                    }
                });
                break;*/
            case R.id.aboutUsLL:
                intent1 = new Intent(SetupActivity.this, AboutUsllActivity.class);
                startActivity(intent1);
                break;
            case R.id.feedbackLL:
                startActivity(intent);
                break;
           /* case R.id.shareLL:
                ShareListener shareListener=new ShareListener() {
                    @Override
                    public void shareSuccess() {
                        showToast("fenxciangchengg");
                    }

                    @Override
                    public void shareFailure(Exception e) {
                        showToast("fenxciangchengg");
                    }

                    @Override
                    public void shareCancel() {
                        showToast("fenxciangchengg");
                    }
                };
                //ShareUtil.shareImage(this, SharePlatform.QQ, "http://www.hxtk.com/cover/14/975420fa-75a7-4f1a-9746-13c041cee849.jpg", shareListener);
                //ShareUtil.shareMedia(this, SharePlatform.QQ, "title", "summary", "targetUrl", "thumb", shareListener);
                break;*/
            case R.id.exitBut:
                isOrNoDialog = new IsOrNoDialog(this, "确定退出登录？", true);

                isOrNoDialog.setListener(new IsOrNoDialog.Listener() {
                    @Override
                    public void okClick() {
                        OkHttpUtils.post(HttpManager.IP+"/person-ClearUserInfo")
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(String s, Call call, Response response) {
                                        try {
                                            JSONObject jsonObject=new JSONObject(s);
                                            if (jsonObject.getInt("returnCode")==200||jsonObject.getInt("returnCode")==400){
                                                UserManageBean.exit(SetupActivity.this);

                                                goBack();
                                            }else {
                                                showToast(jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });

                    }

                    @Override
                    public void closeClick() {

                    }
                });
                break;
        }
    }

    /**
     * 计算文件夹的大小
     */
    public static long getFolderSize(File file) {
        if (!file.exists()) {
            return 0;
        }
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                // 如果下面还有文件
                if (fileList[i].isDirectory()) {
                    size = size + getFolderSize(fileList[i]);
                } else {
                    size = size + fileList[i].length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 格式化得到的总大小 默认是byte,  然后根据传入的大小,自动转化成合适的大小单位
     *
     * @param size
     * @return
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return "0K";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }


}
